import MySql.Goods;
import MySql.User;
import MySql.UserManager;
import MySql.goodsManager;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

public class AddUsersServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out=response.getWriter();

        String username=request.getParameter("username");
        String passwd=request.getParameter("passwd");
        String email=request.getParameter("email");
        String truename=request.getParameter("truename");
        Integer age=Integer.parseInt(request.getParameter("age"));

        out.println(username);
        out.println(passwd);
        out.println(email);
        out.println(truename);
        out.println(age);

        User user=new User(username,passwd,email,truename,age);
        Boolean flag=false;
        Boolean flag1=false;
        out.println(user.toString());
        try {
            flag= UserManager.testId(user);
            out.println(flag);
            if(flag){
                flag1= UserManager.updateUser(user);
                out.println(flag1+"4589");
            }else{
                flag1=UserManager.registerUser(user);
                out.println(flag1+"htfgj");
            }
        } catch (SQLException e) {
            out.println("出错了");
            e.printStackTrace();
        }
        if(flag1){
            //成功界面
            response.sendRedirect("manager.jsp#user");
        }else{
            //跳转失败界面
            response.sendRedirect("fail.jsp");
        }
    }
}
